package com.coding.controller;

import com.coding.base.pojo.BaseModel;
import com.coding.base.pojo.PageQueryModel;
import com.coding.base.util.DateUtil;
import com.coding.pojo.Post;
import com.coding.service.PostService;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @auther wangchenghao
 * @date 2020/7/28
 **/
@Controller
@ResponseBody
@RequestMapping("post")
public class PostController {
    @Autowired
    PostService postService;

    @ApiOperation("根据用户id查询帖子")
    @RequestMapping(value = "/getByUserId", method = {RequestMethod.GET})
    public BaseModel getbyuserid(@ApiParam("用户id") @RequestParam(value = "user_id", required = true) Integer user_id,
                                 @ModelAttribute PageQueryModel pageQueryModel) throws Exception {
        BaseModel model = new BaseModel();
        Post post = new Post();
        if (user_id == null) {
            model.setFailure("传入数据错误");
            return model;
        } else {
            post.setPost_user_id(user_id);
            postService.getlistbyselect(user_id, post, pageQueryModel, model);
        }
        return model;
    }

    @ApiOperation("根据圈子id查询帖子")
    @RequestMapping(value = "/getByGroupId", method = {RequestMethod.GET})
    public BaseModel getbygroupid(@ApiParam("用户id") @RequestParam(value = "user_id", required = true) Integer user_id,
                                  @ApiParam("圈子id") @RequestParam(value = "group_id", required = true) Integer group_id,
                                  @ModelAttribute PageQueryModel pageQueryModel) throws Exception {

        BaseModel model = new BaseModel();
        Post post = new Post();
        if (group_id == null) {
            model.setFailure("传入数据错误");
            return model;
        } else {
            post.setPost_group_id(group_id);
            postService.getlistbyselect(user_id, post, pageQueryModel, model);
        }
        return model;

    }

    @ApiOperation("根据班级id查询帖子")
    @RequestMapping(value = "/getByClassId", method = {RequestMethod.GET})
    public BaseModel getbyclassid(@ApiParam("用户id") @RequestParam(value = "user_id", required = true) Integer user_id,
                                  @ApiParam("班级id") @RequestParam(value = "class_id", required = true) Integer class_id,
                                  @ModelAttribute PageQueryModel pageQueryModel) throws Exception {
        BaseModel model = new BaseModel();
        Post post = new Post();
        if (class_id == null) {
            model.setFailure("传入数据错误");
            return model;
        } else {
            post.setPost_class_id(class_id);
            postService.getlistbyselect(user_id, post, pageQueryModel, model);
        }
        return model;
    }

    @ApiOperation("根据main_place_id查询帖子")
    @RequestMapping(value = "/getByMainId", method = {RequestMethod.GET})
    public BaseModel getByMainId(@ApiParam("用户id") @RequestParam(value = "user_id", required = true) Integer user_id
            , @ApiParam("main_place_id") @RequestParam(value = "main_place_id", required = true) Integer main_place_id
    ) throws Exception {
        BaseModel model = new BaseModel();
        Post post = new Post();
        post.setPost_place_id(main_place_id);
        postService.getlistbyselect(user_id, post, new PageQueryModel(), model);
        return model;
    }

    @ApiOperation("查看热门帖子")
    @RequestMapping(value = "/getHotPost", method = {RequestMethod.GET})
    public BaseModel getHotPost(@ApiParam("用户id") @RequestParam(value = "user_id", required = true) Integer user_id,
                                @ModelAttribute PageQueryModel pageQueryModel) throws Exception {
        BaseModel model = new BaseModel();
        Post post = new Post();
        post.setPost_more1("hot");
        postService.getlistbyselect(user_id, post, pageQueryModel, model);
        return model;
    }

    @ApiOperation("根据帖子id查询帖子")
    @RequestMapping(value = "/getByPostId", method = {RequestMethod.GET})
    public BaseModel getByPostId(@ApiParam("user_id") @RequestParam(value = "user_id", required = true) Integer user_id,
                                 @ApiParam("post_id") @RequestParam(value = "post_id", required = true) Integer post_id
    ) throws Exception {
        BaseModel model = new BaseModel();

        postService.getPostbyselect(user_id, post_id, model);

        return model;
    }

    @ApiOperation("新增帖子")
    @RequestMapping(value = "/addPost", method = {RequestMethod.POST})
    public BaseModel addPost(@ApiParam("class_id") @RequestParam(value = "class_id", required = false) Integer class_id,
                             @ApiParam("group_id") @RequestParam(value = "group_id", required = false) Integer group_id,
                             @ApiParam("user_id") @RequestParam(value = "user_id", required = true) Integer user_id,
                             @ApiParam("title") @RequestParam(value = "title", required = true) String title,
                             @ApiParam("content") @RequestParam(value = "content", required = true) String content,
                             @ApiParam("location_name") @RequestParam(value = "post_more2", required = true) String post_more2,
                             @ApiParam(value = "上传的图片", required = true) MultipartFile file, HttpServletRequest request

    ) throws Exception {
        BaseModel model = new BaseModel();
        Post post = new Post();
        if (class_id != null) {
            post.setPost_class_id(class_id);
        } else {
            post.setPost_class_id(1);
        }
        if (group_id != null) {
            post.setPost_group_id(group_id);
        } else {
            post.setPost_group_id(1);
        }
        post.setPost_user_id(user_id);
        post.setPost_title(title);
        post.setPost_content(content);
        post.setPost_more2(post_more2);
        post.setPost_more1("hot");

        post.setPost_time(DateUtil.getStringDate());
        postService.insertPost(post, file, model);

        return model;
    }

    @ApiOperation("新增帖子(多图)")
    @RequestMapping(value = "/addPostMoreImage", method = {RequestMethod.POST}, consumes = "multipart/*", headers = "content-type=multipart/form-data")
    public BaseModel addPostMoreImage(@ApiParam("class_id") @RequestParam(value = "class_id", required = false) Integer class_id,
                                      @ApiParam("group_id") @RequestParam(value = "group_id", required = false) Integer group_id,
                                      @ApiParam("user_id") @RequestParam(value = "user_id", required = true) Integer user_id,
                                      @ApiParam("title") @RequestParam(value = "title", required = true) String title,
                                      @ApiParam("content") @RequestParam(value = "content", required = true) String content,
                                      @ApiParam("location_name") @RequestParam(value = "post_more2", required = true) String post_more2,
                                      @ApiParam(value = "上传的图片", required = true) @RequestParam(value = "files") MultipartFile[]  files,
                                      HttpServletRequest request

    ) throws Exception {
        BaseModel model = new BaseModel();
        Post post = new Post();
        if (class_id != null) {
            post.setPost_class_id(class_id);
        } else {
            post.setPost_class_id(1);
        }
        if (group_id != null) {
            post.setPost_group_id(group_id);
        } else {
            post.setPost_group_id(1);
        }
        post.setPost_user_id(user_id);
        post.setPost_title(title);
        post.setPost_content(content);
        post.setPost_more2(post_more2);
        post.setPost_more1("hot");
        if (files == null || files.length == 0) {
            model.setFailure("传输图片错误");
            return model;
        }
        post.setPost_time(DateUtil.getStringDate());
        postService.insertPostMoreImage(post, files, model);

        return model;
    }
}
